//
//  CZRefreshView.m
//  XinLang_OC
//
//  Created by WONG on 16/3/19.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZRefreshView.h"


@interface CZRefreshView ()
/**
 *  旋转的箭头
 */
@property (weak, nonatomic) IBOutlet UIImageView *tipIcon;
/**
 *  箭头和下拉刷新数据label的父控件
 */
@property (weak, nonatomic) IBOutlet UIView *tipView;
/**
 *  转轮
 */
@property (weak, nonatomic) IBOutlet UIImageView *loadingView;

@end

@implementation CZRefreshView

+ (instancetype)refreshView {
    return [[NSBundle mainBundle] loadNibNamed:@"CZRefreshView" owner:nil options:nil].lastObject;
}

/**
 *  箭头旋转 isUp: true箭头转上去,false箭头转下来
 */
- (void)tipIconRoration:(BOOL)isUp {
    [UIView animateWithDuration:0.25 animations:^{
        self.tipIcon.transform = isUp ? CGAffineTransformMakeRotation(M_PI - 0.01) : CGAffineTransformIdentity;
    }];
}
/**
 *  开始刷新动画
 */
- (void)startRotation {
    NSString *animationKey = @"rotation";
    
    if ([self.loadingView.layer animationForKey:animationKey]) {
        // 有动画 直接返回
        return;
    }
    
    // 没有动画
    
    self.tipView.hidden = YES;
    
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    // 设置动画参数
    rotation.toValue = @(2 * M_PI);
    rotation.duration = 0.25;
    rotation.repeatCount = MAXFLOAT;
    
    [self.loadingView.layer addAnimation:rotation forKey:animationKey];
}
/**
 *  停止刷新动画
 */
- (void)stopRotation {
    self.tipView.hidden = NO;
    // 移除动画
    [self.loadingView.layer removeAllAnimations];
}



@end



















